package com.baishui.gateway.server;

import com.baishui.gateway.handler.GlobalFailureHandler;
import com.baishui.gateway.handler.InitializeHandler;
import com.baishui.gateway.handler.ServiceManageHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class VertxServer extends AbstractVerticle {

    @Autowired
    private InitializeHandler initializeHandler;

    @Autowired
    private GlobalFailureHandler globalFailureHandler;

    @Autowired
    private ServiceManageHandler serviceManageHandler;


    @Override
    public void start() throws Exception {
        Router router = Router.router(vertx);
        // 500 的状态码
        router.errorHandler(500, globalFailureHandler);
        router.route().handler(RoutingContext::next).failureHandler(globalFailureHandler);

        router.route().handler(initializeHandler);
        router.route().handler(serviceManageHandler);

        vertx.createHttpServer().requestHandler(router).listen(8888);
    }

    @PostConstruct
    private void init() {

    }

}
